package com.wctj.api.business.common.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.wctj.api.base.constant.JwtConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @author carol
 */
@Slf4j
@EnableKnife4j
@EnableSwagger2
@Configuration
@ConditionalOnProperty(name = "enabled", prefix = "mall.swagger",havingValue = "true",matchIfMissing = false)
public class Swagger2Config {

    @Resource
    private Swagger2Property swaggerProperty;

    @ConditionalOnMissingBean(Docket.class)
    @Bean
    public Docket createRestApi() {
        log.info("配置================> swagger文档");
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(requestParameterBuilderList())
                //是否开启 (true 开启  false隐藏。生产环境建议隐藏)
                .enable(swaggerProperty.getEnabled())
                .apiInfo(apiInfo())
                .select()
                //扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
                .apis(RequestHandlerSelectors.basePackage(swaggerProperty.getBasePackage()))
                //指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                //设置文档标题(API名称)
                .title(swaggerProperty.getTitle())
                //文档描述
                .description(swaggerProperty.getDescription())
                //版本号
                .version(swaggerProperty.getVersion())
                .build();
    }

    private List<Parameter> requestParameterBuilderList(){

        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name(JwtConstant.HEADER_TOKEN_CODE).description("令牌token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
//        ParameterBuilder userIdPar = new ParameterBuilder();
//        userIdPar.name(JwtConstant.HEADER_NAME_USER_ID).description("用户Id").modelRef(new ModelRef("int")).parameterType("header").required(false).build();

        List<Parameter> pars = new ArrayList<>();
        pars.add(tokenPar.build());
//        pars.add(userIdPar.build());
        return pars;
                    /*
                Lists.newArrayList(
                parameterUserName
          new RequestParameterBuilder()
                        .name("X-username")
                        .description("username")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("nbg@8531.com")
                                .allowEmptyValue(false))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-realName")
                        .description("realName")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("大洋测试1")
                                .allowEmptyValue(false))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-userId")
                        .description("userId")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("419")
                                .allowEmptyValue(true))
                        .build(),
                
                new RequestParameterBuilder()
                        .name("X-userType")
                        .description("userType")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("0")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-systemCode")
                        .description("systemCode")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("TMY")
                                .allowEmptyValue(true))
                        .build(),

                new RequestParameterBuilder()
                        .name("Authorization")
                        .description("token")
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("bearer abc")
                                .allowEmptyValue(true))
                        .in(ParameterType.HEADER)
                        .build()*/
//        );
    }
}